<template>
  <div class="box">
    <header class="header">
      <van-search
        v-model="keywords"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @clear="keywords = ''"
      >
        <template #left>
          <van-icon name="arrow-left" @click="$router.back()"/>
        </template>
        <template #action>
          <van-button color="#e93b3d" @click="onSearch(keywords)">搜索</van-button>
        </template>
      </van-search>
    </header>
    <div class="content">
      <div v-if="showSearch" class="history">
        <div class="recentsearch">
          <p>最近搜索</p>
          <van-icon @click="clearSearch" name="delete" />
        </div>
        <ul>
          <li v-for="(item,index) of recentsearch" :key="index"><a href="javascript:void(0)" @click="onSearch(item)">{{ item }}</a></li>
        </ul>
      </div>
      <div class="hotsearch">
        <p>热门搜索</p>
        <span v-if="showHot" @click="showHot=false">隐藏</span>
        <span v-else @click="showHot=true">显示</span>
      </div>
      <ul v-show="showHot">
        <li v-for="(item,index) of hotsearch" :key="index"><a href="javascript:void(0)" @click="onSearch(item)">{{ item }}</a></li>
      </ul>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Search, Button, Icon, Dialog } from 'vant'
import { getCategory } from '@/api/search'

Vue.use(Search)
Vue.use(Button)
Vue.use(Icon)
Vue.use(Dialog)

export default {
  data () {
    return {
      keywords: '',
      recentsearch: [],
      hotsearch: [],
      showSearch: false,
      showHot: true
    }
  },
  created () {
    getCategory().then(res => {
      this.hotsearch = res.data.data
    })
    if (localStorage.getItem('recentsearch') !== null) {
      this.recentsearch = JSON.parse(localStorage.getItem('recentsearch'))
      this.showSearch = true
    }
  },
  methods: {
    onSearch (keywords) {
      // const exists = this.recentsearch.some(item => {
      //   return item === this.keywords
      // })
      const exists = this.recentsearch.includes(keywords)
      if (exists) {
        const index = this.recentsearch.indexOf(keywords)
        this.recentsearch.splice(index, 1)
      }
      this.recentsearch.unshift(keywords)
      this.showSearch = true
      localStorage.setItem('recentsearch', JSON.stringify(this.recentsearch))
      // this.$router.push(`/searchlist?keywords=${keywords}`)
      this.$router.push({ path: 'searchlist', query: { keywords } })
    },
    clearSearch () {
      Dialog.confirm({
        message: '确定要清空吗?'
      }).then(() => {
        this.showSearch = false
        this.recentsearch = []
        localStorage.removeItem('recentsearch')
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.van-search {
  height: 0.44rem;
  .van-icon{
    font-size: 18px;
  }
  .van-cell{
    padding: 2px 0;
  }
  .van-button {
    height: 26px;
  }
  .van-button--normal {
    padding: 0 6px;
  }
  .van-search__content.van-search__content--square {
    margin-left: 12px;
  }
  .van-search__action {
    margin-right: 6px;
  }
}
.content{
  overflow: hidden;
  .hotsearch, .recentsearch {
    padding:0 6px;
    display: flex;
    justify-content: space-between;
    p{
      font-size: 15px;
      line-height: 15px;
      color: #232326;
      width: 100px;
      padding-left: 1px;
      margin: 10px;
    }
    .van-icon{
      font-size: 20px;
      color: #686868;
      padding:10px 4px 0;
    }
    span{
      color: #999;
      font-size: 15px;
      line-height: 15px;
      padding: 10px;
    }
  }
  ul{
    display: flex;
    flex-wrap: wrap;
    padding-left:10px;
    li {
      margin: 4px;
      a{
        height: 23px;
        line-height: 24px;
        border-radius: 3px;
        width: 100%;
        color: #686868;
        white-space: nowrap;
        text-overflow: ellipsis;
        background-color: #f0f2f5;
        padding: 4px 13px;
        overflow: hidden;
        box-sizing: border-box;
      }
    }
  }
}

</style>
